package com.gexiaobao.pigeon.ui.fragment.mall.evaluate

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentViewCommentsListBinding
import com.gexiaobao.pigeon.ui.adapter.AllEvaluateAdapter
import com.gexiaobao.pigeon.viewmodel.EvaluateViewModel

/**
 * @Author      : hxw
 * @Date        : 2023/6/29 8:28
 * @Describe    : 查看全部评价列表
 */
class FragmentViewCommentsList : BaseFragment<EvaluateViewModel, FragmentViewCommentsListBinding>() {

    private var evalType: Int = 0
    private val mAdapter: AllEvaluateAdapter by lazy { AllEvaluateAdapter(arrayListOf()) }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getReviewsList(false, 13, evalType)
            }
        }
        mDatabind.includeBar.swipeRefreshLayout.init {
            onRefresh()
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        onRefresh()
    }

    private fun onRefresh() {
        evalType = arguments?.getInt("evalType", 0)!!
        val goodId = arguments?.getInt("goodId", 0)!!
        showLoadingAntiShake(getString(R.string.loading))
        mViewModel.getReviewsList(true, goodId, evalType)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.viewCommentsResult.observe(viewLifecycleOwner) {
            if (mDatabind.includeBar.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeBar.swipeRefreshLayout.isRefreshing = false
            }
            dismissLoadingAntiShake()
            mAdapter.setList(it.list)
        }
    }

    companion object {
        fun newInstance(evalType: Int, goodId: Int): FragmentViewCommentsList {
            val fragment = FragmentViewCommentsList()
            val bundle = Bundle()
            bundle.putInt("evalType", evalType)
            bundle.putInt("goodId", goodId)
            fragment.arguments = bundle
            return fragment
        }
    }
}